//
//  HCStatusViewModel.swift
//  新浪微博
//
//  Created by fanhoucheng on 16/9/27.
//  Copyright © 2016年 fanhoucheng. All rights reserved.
//

import UIKit

class HCStatusViewModel: NSObject {
    
    var avatarImage: UIImage?
    var memberImage: UIImage?
    var iconURL: URL?
    
    //评论的文字
    var comment_text: String?
    //点赞的文字
    var ohYeahText: String?
    //转发的文字
    var repost_text: String?
    //如果有转发视图,就返回转发视图配图,
    var pic_urls: [HCPicUrl]? {
        return status?.retweeted_status == nil ? status?.pic_urls : status?.retweeted_status?.pic_urls
    }

    
    var status: HCStatus? {
        didSet {
            dealAvatarImage()
            dealMemberImage()
            dealHeadURL()
            comment_text = dealToolBarText(count: (status?.comments_count ?? 0), defaultText: "评论")
            ohYeahText = dealToolBarText(count: (status?.attitudes_count ?? 0), defaultText: "赞")
            repost_text = dealToolBarText(count: (status?.reposts_count ?? 0), defaultText: "转发")
        }
    }
    
    func dealToolBarText(count: Int, defaultText: String) -> String {
        
        if count == 0 {
            return defaultText
        }
        
        if count > 10000 {
            return "\(Double(count / 1000) / 10)万"
        }
        return count.description
    }
    
    private func dealHeadURL() {
        let urlString = status?.user?.avatar_large ?? ""
        iconURL = URL(string: urlString)
    }
    
    private func dealAvatarImage() {
        let verified_type = status?.user?.verified_type ?? -1
        switch verified_type {
        case 0:
            avatarImage = #imageLiteral(resourceName: "avatar_vip")
        case 2,3,5:
            avatarImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            avatarImage = #imageLiteral(resourceName: "avatar_grassroot")
            
        default:
            avatarImage = nil
        }

    }
    
    func dealMemberImage() {
        //在if let/var 语句中赋值对象后面跟 ',' ','就可以直接使用前面赋值的对象
        if let mbrank = status?.user?.mbrank, mbrank > 0 && mbrank < 7 {
            let imageName = "common_icon_membership_level\(mbrank)"
            memberImage = UIImage(named: imageName)
        }
    }
}
